package com.futuristik.modes;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Image.Scaling;
import com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle;
import com.futuristik.arkitek.Map;

public class ChallengeMode extends ArkitekMode {


	String mapFolder = "data/maps/";
	Skin skin;
	Stage ui;

	String[] listEntries;
	String[] fileNames;

	public ChallengeMode (Game game) {
		super(game);
	}

	@Override
	public void show () {

		//Access map directory and grab list of maps!
		FileHandle dir = Gdx.files.internal(mapFolder);
		if(!dir.exists())
			dir.mkdirs();

		FileHandle[] files = dir.list();

		String[] entries = new String[files.length];
		fileNames = new String[files.length];

		int count = 0;

		for(FileHandle f :  files){
			Map testMap = new Map();
			if(testMap.loadChallenge(f.name())){
				entries[count] = (count+1)+". "+testMap.name;
				fileNames[count] = f.name();
				count++;
			}
		}	

		listEntries = new String[count];
		System.arraycopy(entries, 0, listEntries, 0, count);

		//Create load game screen

		ui = new Stage(480, 320, true);

		Image background = new Image(new TextureRegion(new Texture(Gdx.files.internal("data/background2.png"))), Scaling.none);
		background.y = 320-512;

		skin = new Skin(Gdx.files.internal("data/uiskin.json"), Gdx.files.internal("data/uiskin.png"));

		final List list = new List(listEntries, skin.getStyle(ListStyle.class), "list");

		//play Game Button
		Button playButton = new Button("Play Map", skin.getStyle(ButtonStyle.class), "button-sl");
		playButton.y = ui.height() - 250;
		playButton.x = 25;
		playButton.width = 100;
		playButton.height = 50;
		playButton.setClickListener(new ClickListener(){
			public void click(Actor actor) {
				game.setScreen(new Play(game, new Map(fileNames[list.getSelectedIndex()], false)));
			}
		});

		ScrollPane scrollPane = new ScrollPane(list, ui, skin.getStyle(ScrollPaneStyle.class), "scroll2");
		scrollPane.width = 300;
		scrollPane.height = 200;
		scrollPane.x = 150;
		scrollPane.y = 20;

		ui.addActor(background);	
		ui.addActor(playButton);
		ui.addActor(scrollPane);

		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		Gdx.input.setInputProcessor(ui);	
	}

	@Override
	public void render (float deltaTime) {
		ui.draw();

		if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
			game.setScreen(new MainMenu(game));
		}
	}

	@Override
	public void pause () {
	}

	@Override
	public void resume () {
	}

	@Override
	public void dispose () {
	}

	@Override
	public void hide () {
		ui.clear();
		ui.dispose();
	}
}